package com.xintonghua.bussiness.ui.fragment.client.user;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.DividerItemDecoration;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ProductAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.ProductBean;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductActivity extends BaseActivity implements XRecyclerView.LoadingListener {

    @BindView(R.id.lv_product)
    XRecyclerView lvProduct;
    ProductAdapter adapter;
    int page = 1;

    //                  bun1.putSerializable("data0", (Serializable) contantbean);
//                bun1.putSerializable("data1", (Serializable) userInfoBean);
    UserInfoBean userInfoBean;
    Contant contantbean;
    List<ProductBean.ListBean> productlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        ButterKnife.bind(this);
        contantbean = (Contant) JumpUtils.getSerializable(this, "data0");
        userInfoBean = (UserInfoBean) JumpUtils.getSerializable(this, "data1");
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("产品", "添加", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                JumpUtils.jumpto(ProductActivity.this, ProductAddActivity.class, contantbean);
            }
        });
        productlist = new ArrayList<>();
        lvProduct.setLoadingMoreEnabled(false);
        lvProduct.setPullRefreshEnabled(true);
        RefreshUtils.initList(this, lvProduct);
        lvProduct.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, MyUtils.dip2px(this, 4), ContextCompat.getColor(this, R.color.grey_bg)));
        adapter = new ProductAdapter(this, productlist);
        lvProduct.setAdapter(adapter);
        lvProduct.setLoadingListener(this);
        getMemberProducts();
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    ProductBean bean = JsonUtil.getEntityByJsonString(data.toString(), ProductBean.class);
                    if (page == 1) {
                        productlist.clear();
                    }
                    productlist.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                    lvProduct.loadMoreComplete();
                    lvProduct.refreshComplete();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getMemberProducts() {
        httpCent.getMemberProducts("" + page, "" + contantbean.getId(), this, MyState.requestOne);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getMemberProducts();
    }

    @Override
    public void onLoadMore() {
        getMemberProducts();
        page++;
    }
}
